import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Http, Headers} from '@angular/http';
import {TurbComponent} from '../entity/turbcomponent.model';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class TurbcomponentService {
  private readonly param = 'basicTurbComponents';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }


  constructor(private http: InterceptorService) {
  }

  /**
   * 获取报警类别
   *
   * @returns
   * @memberof FaultLevelService
   */
  // getTurbComponents(): Promise<TurbComponent[]> {
  //     let turbComponents: TurbComponent[] = [
  //         new TurbComponent(1, "发电机系统", 1, 0),
  //         new TurbComponent(2, "变压器", 2, 0),
  //         new TurbComponent(3, "电控系统", 3, 0),
  //         new TurbComponent(4, "偏航系统", 4, 0),
  //         new TurbComponent(5, "变频系统", 5, 0),
  //         new TurbComponent(6, "变桨系统", 6, 0),
  //         new TurbComponent(7, "机舱及塔架", 7, 0),
  //         new TurbComponent(8, "风轮", 8, 0),
  //         new TurbComponent(9, "液压系统", 9, 0),
  //         new TurbComponent(10, "传动变速系统", 10, 0),
  //         new TurbComponent(11, "电源系统", 11, 0),
  //         new TurbComponent(12, "远程控制系统", 12, 0),
  //         new TurbComponent(13, "通讯系统", 13, 0),
  //         new TurbComponent(14, "其他", 14, 0),

  //     ];
  //     return Promise.resolve(turbComponents);
  // }

  getTurbComponents(): Promise<TurbComponent[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as TurbComponent[])
      .catch(this.handleError);

  }

  getParentTurbComponents(): Promise<TurbComponent[]> {
    return this.http.get(`${this.url}/parent`)
      .toPromise()
      .then(response => response.data as TurbComponent[])
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}


// WEBPACK FOOTER //
// ./src/app/service/turbcomponent.service.ts
